package com.goktech.project.hmw.service.impl;

import com.goktech.project.hmw.dao.ICountyProjectDao;
import com.goktech.project.hmw.mapper.CountyProject;
import com.goktech.project.hmw.service.ICountyProjectService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class CountyProjectServiceImpl implements ICountyProjectService {

	@Autowired
	private ICountyProjectDao countyProjectDao;
	@Autowired
	private StringRedisTemplate redisTemplate;
	/**
	 * 区县总体人数金额
	 *
	 * @return
	 */
	@Override
	public List<CountyProject> listTotal() {
		List<CountyProject> list=null;
		String county="listtotal11";
		if (redisTemplate.hasKey(county)){
			ValueOperations<String, String> operations = redisTemplate.opsForValue();
//			redisTemplate.delete(county);
			String o= operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(o);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.listTotal();
			ValueOperations<String, String> operations = redisTemplate.opsForValue();
			String json = JSONArray.fromObject(list).toString();
			operations.set(county,json);

		}
		return list;
	}

	/**
	 * 区县按项目人数金额
	 *
	 * @return
	 */
	@Override
	public List<CountyProject> listProjectPersonCaption() {
		return countyProjectDao.listProjectPersonCaption();
	}

	/**
	 * 区县按时间人数金额
	 *
	 * @return
	 */
	@Override
	public List<CountyProject> listYearPersonCaption() {
		return countyProjectDao.listYearPersonCaption();
	}

	/**
	 * 区县人数项目人均发放
	 *
	 * @return
	 */
	@Override
	public List<CountyProject> listAvlProjectPersonCaption() {
		return countyProjectDao.listAvlProjectPersonCaption();
	}

	/**
	 * 前十
	 * param year
	 * @return
	 */
	@Override
	public List<CountyProject> listTop10(String year) {
		return countyProjectDao.listTop10(year);
	}

	/**
	 * 福州市总金额人数逐年统计
	 * @return
	 */
	@Override
	public List<CountyProject> listCityTotalCapitalPerson() {
		return countyProjectDao.listCityTotalCapitalPerson();
	}

	/**
	 * 通过区县名称获取区县数据按照时间纬度
	 * @return
	 */
	@Override
	public List<CountyProject> listPersonCapitalYearByCounty(String county) {
		return countyProjectDao.listPersonCapitalYearByCounty(county);
	}

	/**
	 * 通过区县获取区县人数项目人均发放 前top
	 * @param county
	 * @param top
	 * @return
	 */
	@Override
	public List<CountyProject> listAvlProjectPersonCaptionByCounty(String county, String top){
		return countyProjectDao.listAvlProjectPersonCaptionByCounty(county,top);
	}

	/**
	 * 通过区县获取下一级数据
	 * @param county
	 * @return
	 */
	@Override
	public List<CountyProject> listTownshipTotal(String county){
		return countyProjectDao.listTownshipTotal(county);
	}

	/**
	 * 区县按项目人数金额TOP
	 * @param column 排序列
	 * @param top 前几
	 * @return
	 */
	@Override
	public List<CountyProject> listProjectPersonCaptionTop(String column,String top) {
		return countyProjectDao.listProjectPersonCaptionTop(column,top);
	}

	/**
	 * 按项目地区获取前top
	 * @param type
	 * @param top
	 * @return
	 */
	@Override
	public List<CountyProject> listRight2(String type,String top){
		return countyProjectDao.listRight2(type,top);
	}

	/**
	 * 通过区县获取区县人数项目人均发放前top当前年
	 * @param county
	 * @param top
	 * @return
	 */
	@Override
	public List<CountyProject> listAvlProjectPersonCaptionTopByCounty(String county, String top){
		return countyProjectDao.listAvlProjectPersonCaptionTopByCounty(county,top);
	}

	/**
	 * 右1
	 * @param column 排序列
	 * @param top 前几
	 * @return
	 */
	@Override
	public List<CountyProject> listRight1(String column,String top){
		return countyProjectDao.listRight1(column,top);
	}

	/**
	 * 获取项目的总金额总人数项目参与的总区县
     */
	@Override
	public List<CountyProject> allListProject() {
		List<List<CountyProject>> listAll=null;
		List<CountyProject> list=null;
		String county="allListProject";
		if (redisTemplate.hasKey(county)){
			ValueOperations<String, String> operations = redisTemplate.opsForValue();
//			redisTemplate.delete(county);
			String o= operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(o);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.allListProject();
			ValueOperations<String, String> operations = redisTemplate.opsForValue();
			String json = JSONArray.fromObject(list).toString();
			operations.set(county,json);

		}
		return list;
	}

	/**
	 * 根据地区查找项目总人数金额
	 * @param
     * @return
     */
	@Override
	public List<CountyProject> allListCounty(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String county="";
		if(reqMap.get("type")!=null && reqMap.get("type")!=""){
			county+="allListCounty"+reqMap.get("county")+reqMap.get("type");
		}else{
			county+="allListCounty"+reqMap.get("county");
		}
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.allListCounty(reqMap);
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;
	}

	/**
	 * 所有区县人均发放金额（当前年）
	 * @return
     */
	@Override
	public List<CountyProject> avlAllListCounty() {
		List<CountyProject> list=null;
		String county="avlAllList";
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.avlAllListCounty();
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> pitchListProject(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String project="";
		if(reqMap.get("project")!=null && reqMap.get("project")!=""){
			project+="pitchListProject"+reqMap.get("project")+reqMap.get("type");
		}else{
			project+="pitchListProject"+reqMap.get("project");
		}
		if (redisTemplate.hasKey(project)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(project,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.pitchListProject(reqMap);
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(project,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> yearList(String top) {
		return countyProjectDao.yearList(top);
	}

	@Override
	public List<CountyProject> townshipList(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String county="townshipList"+reqMap.get("county");
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.townshipList(reqMap.get("county"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> topCountyList(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String county="topCountyList"+reqMap.get("county")+reqMap.get("top");
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.topCountyList(reqMap.get("county"),reqMap.get("top"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> toppitchListProject(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String project="topProjectList"+reqMap.get("project")+reqMap.get("top");
		if (redisTemplate.hasKey(project)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(project,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectDao.toppitchListProject(reqMap.get("project"),reqMap.get("top"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(project,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> topYearList(String project,String top) {
		return countyProjectDao.topYearList(project,top);
	}

	@Override
	public List<CountyProject> topTotalProjectList(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String project="topTotalProjectList"+reqMap.get("project");
		if (redisTemplate.hasKey(project)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(project,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list= countyProjectDao.topTotalProjectList(reqMap.get("project"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(project,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> topTotalCountyList(String county, String top) {
		return countyProjectDao.topTotalCountyList(county,top);
	}

	@Override
	public List<CountyProject> totalTownshipList(String county, String project) {
		return countyProjectDao.totalTownshipList(county,project);
	}

	@Override
	public List<CountyProject> indexAmplification(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String key="indexAmplification"+reqMap.get("year");
		if (redisTemplate.hasKey(key)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(key,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list= countyProjectDao.indexAmplification(reqMap.get("year"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(key,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> amplificationCounty(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String key="amplificationCounty"+reqMap.get("year")+reqMap.get("county");
		if (redisTemplate.hasKey(key)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(key,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list= countyProjectDao.amplificationCounty(reqMap.get("year"),reqMap.get("county"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(key,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> amplificationProject(Map<String,String> reqMap) {
		List<CountyProject> list=null;
		String key="amplificationProject"+reqMap.get("year")+reqMap.get("project");
		if (redisTemplate.hasKey(key)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(key,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list= countyProjectDao.amplificationProject(reqMap.get("year"),reqMap.get("project"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(key,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> townMap(Map<String, String> reqMap) {
		List<CountyProject> list=null;
		String key="townMap"+reqMap.get("project");
		if(!StringUtils.isEmpty(reqMap.get("county"))){
			key+=reqMap.get("county");
		}
		//String key="amplificationProject"+reqMap.get("year")+reqMap.get("project");
		if (redisTemplate.hasKey(key)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(key,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list= countyProjectDao.townMap(reqMap);
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(key,json);
		}
		return list;
	}

	@Override
	public List<CountyProject> allproject() {
		return countyProjectDao.allproject();
	}

	@Override
	public List<CountyProject> zongCounty() {
		return countyProjectDao.zongCounty();
	}
	@Override
	public List<CountyProject> zongProject() {
		return countyProjectDao.zongProject();
	}
}
